<template>
  <div class="chat-home-warpper">
    <div class="main-area">
      <div class="logo">
        <img
          src="@/assets/img/logo.png"
          style="object-fit: contain" />
      </div>
      <div class="title">挑选一个提示词</div>
      <div class="btn-group">
        <el-button @click="checkAllPrompts">查看全部</el-button>
        <el-button
          type="primary"
          @click="createNewChat"
          >直接开始</el-button
        >
      </div>
    </div>
    <div class="prompt-info">
      <div
        class="prompt-info-row"
        v-for="(item, index) in allPrompts"
        :key="index">
        <div
          class="prompt-item"
          :title="el.name"
          v-for="(el, elIndex) in item"
          :key="elIndex"
          @click="handleClickPrompt(el)">
          {{ el.name }}
        </div>
        <!--  <div
          class="prompt-item"
          :title="item.name"
          v-for="(item, index) in allPrompts"
          :key="index"
          @click="handleClickPrompt(item)">
          {{ item.name }}
        </div> -->
      </div>
    </div>
  </div>
</template>

<script setup>
  import { getCurrentInstance, ref, onMounted } from 'vue'
  import useAiChat from '@/store/aiChat'

  const aiChatStore = useAiChat()
  // const emit = defineEmits(])
  const { proxy } = getCurrentInstance()
  const props = defineProps({})
  const allPrompts = ref([])
  /* ========方法========= */
  const handleAllPromptsData = (array, size) => {
    if (!array?.length) {
      return [[]]
    }
    // if (array.length < size) {
    //   return array
    // } else {
    const result = []
    for (let i = 0; i < array.length; i += size) {
      result.push(array.slice(i, i + size))
    }
    return result
    // }
  }
  const checkAllPrompts = () => {
    aiChatStore.checkAllPrompts()
  }
  const createNewChat = () => {
    aiChatStore.createNewChat()
  }
  const handleClickPrompt = promptInfo => {
    aiChatStore.jumpToChatWithRole(promptInfo)
  }
  const getAllPrompts = async () => {
    try {
      const res = await proxy.$http.getWithUrl('prompt/query', {
        content: ''
      })
      allPrompts.value = handleAllPromptsData(res, 12)
    } catch (error) {
      console.log(error)
    }
  }
  onMounted(() => {
    getAllPrompts()
  })
</script>

<style scoped lang="scss">
  .chat-home-warpper {
    @include wh100();
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    overflow-x: hidden;
    box-sizing: border-box;
    & > .main-area {
      padding-top: pxTvh(110);
      height: pxTvh(350);
      display: flex;
      flex-direction: column;
      align-items: center;
      // justify-content: center;
      background-image: url('@/assets/img/page/chatbg.png');
      background-repeat: no-repeat;
      background-size: cover;
      & > .logo {
        width: pxTvw(40);
        height: pxTvw(40);
        & > img {
          @include wh100();
        }
      }
      & > .title {
        font-weight: 600;
        @include computed_font(36);
        margin: pxTvh(32) 0 pxTvh(40) 0;
      }
      & > .btn-group {
        display: flex;
        justify-content: end;
        .el-button {
          height: pxTvh(40);
        }
      }
    }
    & > .prompt-info {
      flex: 1;
      display: flex;
      flex-direction: column;
      overflow-y: scroll;
      overflow-x: hidden;
      justify-content: center;
      color: #cfdbfc;
      mask-image: linear-gradient(180deg, transparent, #000, transparent);
      // webkit-mask-image: linear-gradient(180deg, transparent, #000, transparent);
      & > .prompt-info-row {
        display: flex;
        // justify-content: space-between;
        place-content: flex-end;
        width: 100%;
        margin-bottom: pxTvh(24);
        justify-content: center;
        & > .prompt-item {
          cursor: pointer;
          flex-shrink: 0;
          padding: 0 pxTvw(32);
          color: #cfdbfc;
          box-sizing: border-box;
          height: pxTvh(52);
          line-height: pxTvh(52);
          background: #1d2a4b;
          border-radius: pxTvw(8);
          border: 1px solid #3a5592;
          margin-right: pxTvw(24);
          transition: all 0.3s ease;
          max-width: calc(300 / 1080 * 100vh);
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
        }
        & > .prompt-item:hover {
          background-color: #3a5592;
          transform: scale(1.1);
          border: 1px solid rgb(55, 125, 255);
        }
      }
      & > .prompt-info-row:nth-child(2n) {
        margin-left: pxTvw(50);
      }
    }
  }
</style>
